<template>
  <div class="product">
    <!-- 图片 -->
    <div class="winphoto">
      <img :src="detailsdata.large_img" alt="" />
    </div>

    <div class="detailsbox">
      <!-- 名字 -->
      <div class="name">
        {{ detailsdata.name }}
        <div class="price">￥{{ detailsdata.price }}</div>
      </div>

      <div class="name add">{{ detailsdata.enname }}</div>

      <!-- 包含物 -->
      <div class="contain">
        <div class="containbox hot" v-show="detailsdata.tem === ''">
          可做{{ detailsdata.tem || "冷" }}饮
        </div>
        <div class="containbox bean">含有{{ detailsdata.milk_desc }}制品</div>
      </div>

      <!-- 商品简介 -->
      <div class="detailstext">
        {{ detailsdata.desc }}
      </div>

      <!-- 温度\糖 -->
      <div class="specs" v-for="(item, index) in rulesData" :key="index">
        <div class="specstext">{{ item.title }}</div>

        <div
          class="specsbox"
          :class="{ active: item.activeIndex === i }"
          v-for="(v, i) in item.rule"
          :key="i"
          @click="selectRule(item, i)"
        >
          {{ v.title }}
        </div>
      </div>

      <!-- 选择数量 -->
      <div class="num">
        <div class="numtext">选择数量</div>
        <div class="numbox">
          <van-stepper
            v-model="value"
            theme="round"
            button-size="18"
            disable-input
          />
        </div>
      </div>

      <!-- 购买导航栏 -->
      <van-goods-action>
        <van-goods-action-icon
          icon="bag-o"
          text="购物袋"
          :badge="shopbagCount > 0 ? shopbagCount : ''"
          @click="goArrorder"
        />
        <van-goods-action-icon
          icon="like"
          :text="isLike ? '已收藏' : '未收藏'"
          :color="isLike ? 'red' : '#595c5a'"
          @click="LikeAndNotlike"
        />
        <van-goods-action-button
          type="warning"
          text="加入购物袋"
          color="#dba871"
          @click="addShopbag"
        />
        <van-goods-action-button
          type="danger"
          text="立即购买"
          color="#dba224"
        />
      </van-goods-action>
    </div>
  </div>
</template>

<script>
export default {
  name: "Product",

  data() {
    return {
      //商品数量
      value: 1,

      // 商品pid
      pid: "",

      //商品详情数据
      detailsdata: [],

      //规格数据
      rulesData: [],

      //是否收藏
      isLike: false,

      //购物袋数量
      shopbagCount: 0,
    };
  },

  props: ["arr"],

  watch: {
    arr() {
      this.pid = this.arr;
      // 

      this.getProductDetail(this.pid);

      //获取商品收藏信息
      this.getCurrentLike(this.pid);

      this.getShopbagCount();
    },
  },

  methods: {
    //获取商品详情数据
    getProductDetail(pid) {
      this.axios({
        method: "get",
        url: "/productDetail",
        params: {
          pid,
        },
      })
        .then((res) => {
          // 
          let data = res.data.result[0];

          //处理商品规格
          let rules = ["milk", "cream", "sugar", "tem"];

          rules.forEach((v) => {
            if (data[v] === "") {
              return;
            }

            let r = {
              title: data[v + "_desc"],
              activeIndex: 0,
              rule: [],
            };

            let rText = data[v].split(/\//);
            rText.forEach((v) => {
              let subR = {
                title: v,
              };
              r.rule.push(subR);
            });

            this.rulesData.push(r);
          });

          // 

          this.detailsdata = data;
          // 
        })
        .catch((err) => {
          
        });
    },

    //选择规格
    selectRule(item, i) {
      if (item.activeIndex === i) {
        return;
      }
      item.activeIndex = i;
    },

    //获取用户的当前收藏商品
    getCurrentLike(pid) {
      this.axios({
        method: "get",
        url: "/findlike",
        params: {
          pid,
        },
      })
        .then((res) => {
          // 

          // 如果用户登录，处理收藏
          if (res.data.code === 1000) {
            if (res.data.result.length > 0) {
              this.isLike = true;
            }
          }
        })
        .catch((err) => {
          
        });
    },

    //收藏或者取消收藏
    LikeAndNotlike() {
      if (this.isLike) {
        //取消
        this.notlike();
      } else {
        //收藏
        this.like();
      }
    },

    //收藏商品
    like() {
      this.axios({
        method: "post",
        url: "/like",
        data: {
          pid: this.pid,
        },
      })
        .then((res) => {
          // 

          this.$toast(res.data.msg);

          //如果用户没有登录，则跳转到登录页面
          if (res.data.code == 700) {
            setTimeout(() => {
              this.$router.push({ name: "Login" });
            }, 1000);
          }

          //收藏成功
          if (res.data.code === 800) {
            this.isLike = true;
          }
        })
        .catch((err) => {
          
        });
    },

    //取消收藏
    notlike() {
      this.axios({
        method: "post",
        url: "/notlike",
        data: {
          pid: this.pid,
        },
      })
        .then((res) => {
          // 
          this.$toast(res.data.msg);

          //如果用户没有登录, 则跳转到登录页面
          if (res.data.code === 700) {
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 900 && res.data.result === 1) {
            this.isLike = false;
          }
        })
        .catch((err) => {
          
        });
    },

    //获取购物袋商品数量
    getShopbagCount() {
      let tokenString = sessionStorage.getItem("token");
      this.axios({
        method: "get",
        url: "/shopcartCount",
        params: {
          tokenString,
        },
      })
        .then((res) => {
          // 

          if (res.data.code === 4000 && res.data.result !== null) {
            this.shopbagCount = res.data.result;
          }
        })
        .catch((err) => {
          
        });
    },

    //将商品加入购物袋
    addShopbag() {
      // 

      //获取商品规格
      let rule = [];
      this.rulesData.forEach((v) => {
        rule.push(v.rule[v.activeIndex].title);
      });

      // 

      let tokenString = sessionStorage.getItem("token");

      //参数
      let data = {
        count: this.value,
        rule: rule.join("/"),
        pid: this.pid,
        tokenString,
      };

      // 

      this.axios({
        method: "post",
        url: "/addShopcart",
        data,
      })
        .then((res) => {
          // 
          this.$toast(res.data.msg);

          //未登录添加购物袋，则返回登录界面
          if (res.data.code === 700) {
            setTimeout((v) => {
              this.$router.push({ name: "Login" });
            }, 2000);
          }

          //添加购物袋数量
          if (res.data.code === 3000) {
            this.shopbagCount += this.value;
          }
        })
        .catch((err) => {
          
        });
    },

    //点击购物袋跳转到的页面
    goArrorder() {
      this.$router.push({ name: "Arrorder" });
    },
  },
};
</script>

<style lang="less" scoped>
.product {
  margin-top: -20px;
}

.winphoto {
  width: 100%;
  height: 180px;
}

.winphoto img {
  width: 100%;
  height: 180px;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}

.detailsbox {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  padding: 10px;
  margin-bottom: 52px;
}

.name {
  width: 100%;
  height: 20px;
  line-height: 20px;
  font-size: 16px;
  color: #37343b;
  text-align: left;
  display: flex;
  padding: 0 0 4px 0;
  &.add {
    font-size: 12px;
    color: #898989;
  }
}

.price {
  width: 40px;
  height: 20px;
  text-align: center;
  color: #dcab77;
  position: relative;
  left: 130px;
  font-weight: 600;
}

.contain {
  width: 100%;
  display: flex;
}

.containbox {
  width: 80px;
  font-size: 12px;
  text-align: center;
  padding: 0 4px;
  transform: scale(0.7);
  position: relative;
  right: 10px;
  &.hot {
    color: #e1aa66;
    background-color: #fbf5ed;
  }
  &.bean {
    color: #9a9a9a;
    background-color: #f3f3f3;
    position: relative;
    right: 20px;
  }
}

.detailstext {
  width: 306px;
  font-size: 12px;
  color: #898989;
  padding: 6px 10px 0 0;
}

.specs {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}

.specstext,
.numtext {
  width: 100%;
  color: #898989;
  font-size: 12px;
  padding: 10px 0 4px 0;
}

.specsbox {
  width: 60px;
  height: 16px;
  line-height: 16px;
  text-align: center;
  border-radius: 6px;
  background-color: #f5f5f7;
  color: #5d5d5d;
  padding: 4px 0;
  &.active {
    background-color: #ffeedc;
    color: #e0a55d;
  }
  &:nth-child(3) {
    margin-left: 20px;
  }
  &:nth-child(4) {
    margin-left: 20px;
  }
}

.num {
  width: 100%;
  padding: 10px 0 0 0;
  display: flex;
  align-items: center;
}

.numbox {
  width: 150px;
}

/deep/.van-stepper--round .van-stepper__plus {
  background-color: #dba871;
}

/deep/.van-stepper--round .van-stepper__minus {
  color: #dba871;
  border: 0.02667rem solid #dba871;
}

/deep/.van-goods-action {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}

/deep/.van-goods-action-icon {
  color: #818181;
}

/deep/.van-goods-action-button {
  height: 34px;
  font-size: 12px;
}
</style>